package Leo.utils;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class CtmSmsUtil {
    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm");

    public static final int APPT_SUCCESS = 0;
    public static final int APPT_REMINDER = 1;
    private static final String CTM_URL = "http://202.175.4.180:20081/servlet/SendSMS";

    private static final String USERNAME = "Southmax";
    private static final String PASSWORD = "S0uth128";
    private static final String SENDER = "853660128322283";
    private static final String PORT = "20081";
    private String url = "";

    public String sendSms(String phoneNumber, String patientName, String patientEngName, String drName, String drEngName, Date date, int smsType) throws IOException {
//        phoneNumber = AliSmsUtil.formatPhoneNumber(phoneNumber);
        String time = sdf1.format(date);
        String eTime = sdf2.format(date) + " at " + sdf3.format(date);
        String context = "";
        if (smsType == APPT_SUCCESS) {
            context = buildApptContext(patientName, patientEngName, drName, drEngName, time, eTime);
        } else if (smsType == APPT_REMINDER) {
            context = buildReminderContext(patientName, patientEngName, drName, drEngName, time, eTime);
        }
        return getToCtm(phoneNumber, context);
    }

    public String sendSms(String phoneNumber, String context) throws IOException {
        System.out.println(context);
        return getToCtm(phoneNumber, context);
    }

    @PostConstruct
    public void init() {
        StringBuilder sb = new StringBuilder();
        sb.append(CTM_URL).append("?");
        sb.append("username=").append(USERNAME).append("&");
        sb.append("password=").append(PASSWORD).append("&");
        sb.append("from=").append(SENDER).append("&");
        sb.append("report=").append("Y").append("&");
        sb.append("dcs=").append("8").append("&");
        sb.append("locale=").append("utf-8").append("&");
        url = sb.toString();
    }

    static String formatPhoneNumber(String phoneNumber) {
        int length = phoneNumber.length();
        //处理大陆号码
        if (length == 11) {
            String substring = phoneNumber.substring(0, 1);
            if (substring.equals("1")) {
                return "86"+ phoneNumber;
            }
        }
        if (length == 14) {
            String substring = phoneNumber.substring(0, 3);
            if (substring.equals("+86")) {
                return phoneNumber.substring(3);
            }
        }
        //处理澳门号码
        if(length == 8 && "6".equals(phoneNumber.substring(0,1))){
            return "00853" + phoneNumber;
        }
        if(length == 12 && "+853".equals(phoneNumber.substring(0,4))){
            return "00853" + phoneNumber.substring(4);
        }
        if("+".equals(phoneNumber.substring(0,1))){
            return phoneNumber.substring(1);
        }

        return phoneNumber;
    }

    public static void main(String[] args) throws IOException {
        CtmSmsUtil ctmSmsUtil = new CtmSmsUtil();
        ctmSmsUtil.init();
        String s = ctmSmsUtil.sendSms("8613302889085", "高达", "genden", "陈主任", "dr chen", new Date(), CtmSmsUtil.APPT_SUCCESS);
        System.out.println(s);
    }
    /**
     * 发送Get请求到CTM
     *
     * @param phoneNumber
     * @param context
     * @return
     * @throws IOException
     */
    private String getToCtm(String phoneNumber, String context) throws IOException {
        StringBuilder sb = new StringBuilder();
//    sb.append(CTM_URL).append("?");
//    sb.append("username=").append(USERNAME).append("&");
//    sb.append("password=").append(PASSWORD).append("&");
//    sb.append("from=").append(SENDER).append("&");
//    sb.append("to=").append(phoneNumber).append("&");
//    sb.append("report=").append("Y").append("&");
//    sb.append("text=").append(context).append("&");
//    sb.append("dcs=").append("8").append("&");
//    sb.append("locale=").append("utf-8");
        sb.append(url);
        sb.append("to=").append(phoneNumber).append("&");
        sb.append("text=").append(context);
        String fullUrl = sb.toString();
        System.out.println(fullUrl);
        String request = HttpUtil.getRequest(fullUrl);
        return request;
    }

    /**
     * 预约成功的短信
     *
     * @return
     */
    private String buildApptContext(String patientName, String patientEngName, String drName, String drEngName, String date, String eDate) {
        String context = "【澳門銀葵醫院】通知閣下：" + patientName + " 成功預約" + drName + "醫生，" + date + "。請提前到達醫院就診。\n" +
                "[Macau Yin Kui Hospital] Booking confirmation: " + patientEngName + " booking appointment with doctor " + drEngName +
                " on " + eDate + " has been confirmed. Please arrive ahead of schedule.";
        return context;
    }
//【澳門銀葵醫院】通知閣下：陳大文 成功預約翁家權醫生，2018年10月12日 15：00。請提前到達醫院就診。
//            [Macau Yin Kui Hospital] Booking confirmation:Chan Tai Man booking appointment with doctor Dr. Daniel Yung on 2018-10-12 at15:00 has been confirmed. Please arrive ahead of schedule.
//
//            預約提醒短信（就診前24小時發送）：
//            【澳門銀葵醫院】溫馨提示：陳大文 已預約翁家權醫生，2018年10月12日 15：00。請提前到達醫院就診。
//            [Macau Yin Kui Hospital] Booking reminder: ChanTai Man has booking appointment with doctor Dr. Daniel Yung on 2018-10-12 at15:00. Please arrive ahead of schedule.

    /**
     * 提醒就诊的短信
     *
     * @return
     */
    private String buildReminderContext(String patientName, String patientEngName, String drName, String drEngName, String date, String eDate) {
        String context = "【澳門銀葵醫院】溫馨提示：" + patientName + "已預約" + drName + "醫生，" + date + "。請提前到達醫院就診。\n" +
                "[Macau Yin Kui Hospital]Booking reminder: " + patientEngName + " has booking appointment with doctor " + drEngName + " on " +
                eDate + ". Please arrive ahead of schedule.";
        return context;
    }
}
